package code.google.com.analyticsbot;

import java.util.Collection;
import java.util.logging.Logger;
import code.google.com.analyticsbot.data.UserData;
import code.google.com.analyticsbot.data.WaveData;
import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.Gadget;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;

public class EventAnalyzer
{
	private static final String ROBOT_NAME = "analyticsbot";
	private static final String GADGET_XML = "http://" + ROBOT_NAME + ".appspot.com/resources/gadget.xml";
	private static final String ROBOT_USER_ID = "analyticsbot@appspot.com";
	
	private static final Logger log = Logger.getLogger(AnalyticsbotServlet.class.getName());
	

	private WaveData m_data;
	private Blip m_blip = null;

	public EventAnalyzer(RobotMessageBundle events, WaveDataProvider datap)
	{
		m_data = datap.getData();

		Wavelet wavelet = events.getWavelet();
		String waveId = wavelet.getWaveId();
		String waveletId = wavelet.getWaveletId();

		String blipId = m_data.getAnalyticsbotBlipId();
		if(blipId != null)
		{
			log.warning("getting gui blip " + blipId);
			m_blip = events.getBlip(waveId, waveletId, blipId);
			if(m_blip != null)
			{
				if(!m_blip.isDocumentAvailable())
				{
					log.warning("gui blip is unavailable");
					m_blip = null;
				}
			}
		}
		else
		{
			log.warning("gui blip id is null");
		}

		if(m_blip == null)
		{
			// create analytics gui blip
			m_blip = wavelet.getRootBlip().createChild();
			m_data.setAnalyticsbotBlipId(m_blip.getBlipId());

			Gadget gadget = new Gadget(GADGET_XML);
			gadget.setField("waveId", waveId);
			gadget.setField("waveletId", waveletId);
			TextView textView = m_blip.getDocument();
			textView.insertElement(0, gadget);

			log.warning("created gui blip id=" + m_blip.getBlipId());
		}
	}

	public void processEvent(Event e)
	{
		String modifierId = e.getModifiedBy();
		UserData modifier = (modifierId != null) ? m_data.getUserData(modifierId) : null;

		switch(e.getType())
		{
			case WAVELET_BLIP_CREATED:
			{
				modifier.blipCreated();
				break;
			}

			case BLIP_CONTRIBUTORS_CHANGED:
			{
				modifier.blipModified();
				break;
			}

			case WAVELET_PARTICIPANTS_CHANGED:
			{
				try
				{
					Collection<String> participants = e.getAddedParticipants();
					if(participants != null)
					{
						for(String participant : participants)
						{
							UserData user = m_data.getUserData(participant);
							user.setIsParticipant(true);
							if(ROBOT_USER_ID.equalsIgnoreCase(participant))
							{
								user.setIsParticipant(false);							
							}
							if(modifier!=null){
								user.setInviter(modifierId);
							}
							else{
								log.warning("User "+participant+" added but has no owner");
							}
						}
					}
				}
				catch(Exception ex){
					log.warning("Received an exception while attempting to get Removed Participents");
				}
				
				try
				{
					Collection<String> removedParticipants = e.getRemovedParticipants();
					if(removedParticipants != null)
					{
						for(String participant : removedParticipants)				
						{
							UserData user = m_data.getUserData(participant);
							user.setIsParticipant(false);
						}
					}
				}
				catch(Exception ex){
					log.warning("Received an exception while attempting to get Removed Participents");
				}
				break;
			}
			case BLIP_SUBMITTED:
			{
				long timestamp = e.getTimestamp();
				modifier.blipSubmitted(timestamp);
				break;
			}
			default:
			{
				log.warning("UNEXPECTED EVENT: " + e.getType().toString());
			}
		}
	}
}
